﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using IndieStudio.BugsBang.Utility;

#pragma warning disable 0168 // variable declared but not used.

///Developed By Indie Studio
///https://assetstore.unity.com/publishers/9268
///www.indiestd.com
///info@indiestd.com

namespace IndieStudio.BugsBang.Game
{
	[DisallowMultipleComponent]
	[ExecuteInEditMode]
	public class Level : MonoBehaviour
	{
		/// <summary>
		/// The number text of the level.
		/// </summary>
		public Text numberText;

		/// <summary>
		/// The stars of the level.
		/// </summary>
		public Image starOne, starTwo, starThree;

		/// <summary>
		/// The button of the level.
		/// </summary>
		public Transform button;

		/// <summary>
		/// Whether the level is locked or not.
		/// </summary>
		public bool isLocked;

		/// <summary>
		/// The ID of the level.
		/// </summary>
		public int ID;

		/// <summary>
		/// Whether the resources of the level visible in the screen or not.
		/// </summary>
		private bool resourcesVisible = true;

		/// <summary>
		/// The ID of the selected level by user.
		/// </summary>
		public static int SelectedLevelID;

		// Use this for initialization
		void Start ()
		{
			Init ();
		}

		/// <summary>
		/// Init the level.
		/// </summary>
		private void Init ()
		{
			//Set up references
			if (button == null) {
				button = transform.Find ("Button");
			}
				
			if (numberText == null) {
				numberText = transform.Find ("Number").GetComponent<Text> ();
			}

			if (starOne == null) {
				starOne = transform.Find ("Stars").Find ("StarOne").GetComponent<Image> ();
			}

			if (starTwo == null) {
				starTwo = transform.Find ("Stars").Find ("StarTwo").GetComponent<Image> ();
			}

			if (starThree == null) {
				starThree = transform.Find ("Stars").Find ("StarThree").GetComponent<Image> ();
			}

			try {
				//Set up number,ID of the level
				int numberValue = -1;
				if (int.TryParse (name.Split ('-') [1], out numberValue)) {
					ID = numberValue;

					if (numberText != null)
						numberText.text = ID.ToString ();
					else
						Debug.LogWarning (name + ":Number text reference is undefined");
				}

				if (numberValue == -1)
					Debug.LogWarning (name + ":Wrong ID for this level");


				button.GetComponent<Button> ().onClick.AddListener (() => UIEvents.instance.ShowPlayConfirmDialog (ID));
			} catch (Exception ex) {
				Debug.LogError (name + ":Wrong level name, use standard name <b>Level-ID</b>");
			}

			//When app is playing
			if (Application.isPlaying) {

				if (ID > 0 && ID <= LevelsManager.instance.levels.Count) {
					
					LevelsManager.Level levelsManagerLevel = LevelsManager.instance.levels [ID - 1];

					//Create hight light gameobject on last reached level
					if (levelsManagerLevel.isLastRreached) {
						Levels.instance.MoveNearByLevel (this);
						if(!Levels.lerpToLastReached)
							CreateHighLight ();
						Levels.lerpToLastReached = false;
					}

					//Set level locked or not
					isLocked = levelsManagerLevel.isLocked;
					if (isLocked) {
						SetLocked ();
					} else {
						CommonUtil.SetStarsRating (levelsManagerLevel.starsNumber, starOne, starTwo, starThree, Levels.instance.starOn, Levels.instance.starOff);
						SetUnLocked ();
					}
				}
			}
		}

		/// <summary>
		/// Set level as locked.
		/// </summary>
		public void SetLocked ()
		{
			//starOne.transform.parent.gameObject.SetActive (false);
			button.GetComponent<Image> ().sprite = Levels.instance.levelLocked;
			button.GetComponent<Button> ().interactable = false;
		}

		/// <summary>
		/// Set level as unlocked.
		/// </summary>
		public void SetUnLocked ()
		{
			//starOne.transform.parent.gameObject.SetActive (true);
			button.GetComponent<Image> ().sprite = Levels.instance.levelUnLocked;
			button.GetComponent<Button> ().interactable = true;
		}

		/// <summary>
		/// Create the highlight/selected effect behind the level.
		/// </summary>
		public void CreateHighLight ()
		{
			if (transform.Find ("HighLight") != null) {
				return;
			}
		
			if (PrefabsManager.instance.highLightPrefab == null) {
				Debug.LogWarning ("HighLight prefab is undefined in Levels component");
				return;
			}
			GameObject hightLight = Instantiate (PrefabsManager.instance.highLightPrefab, Vector3.zero, Quaternion.identity) as GameObject;
			hightLight.transform.SetParent (transform);
			hightLight.transform.SetSiblingIndex (0);
			hightLight.name = "HighLight";
			RectTransform rectTransform = hightLight.GetComponent<RectTransform> ();
			rectTransform.offsetMin = rectTransform.offsetMax = rectTransform.anchoredPosition3D = Vector3.zero;
			rectTransform.localScale = Vector3.one;

			//Set the parent,sibling index of 'You Here' marker
			Transform youHere = hightLight.transform.Find ("YouHere");
			youHere.SetParent (transform.parent);
			youHere.localScale = Vector3.one;
			Vector3 tempPos = hightLight.transform.position;
			tempPos.y += 0.88f;
			youHere.transform.position = tempPos;
			youHere.GetComponent<RectTransform> ().SetAsLastSibling ();
		}

		// Update is called once per frame
		void Update ()
		{
			if (!Application.isPlaying) {
				Init ();
			}
		}

		/// <summary>
		/// Hide the resources of the level.
		/// </summary>
		public void HideResources ()
		{
			if (!resourcesVisible) {
				return;
			}
			button.gameObject.SetActive (false);
			numberText.gameObject.SetActive (false);
			starOne.transform.parent.gameObject.SetActive (false);
			resourcesVisible = false;
		}

		/// <summary>
		/// Show the resources of the level.
		/// </summary>
		public void ShowResources ()
		{
			if (resourcesVisible) {
				return;
			}

			button.gameObject.SetActive (true);
			numberText.gameObject.SetActive (true);
			starOne.transform.parent.gameObject.SetActive (true);
			resourcesVisible = true;
		}

        public void TEXT1()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }
        public void TEXT2()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }
        public void TEXT3()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT4()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT5()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT6()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT8()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT9()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT10()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT11()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT12()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT13()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT14()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT15()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT16()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }

        public void TEXT17()
        {
            int textNum = 1;
            if (textNum == 1)
            {
                textNum = 2;
            }
        }
    }
}